var fs = require('fs');
var cheerio = require('cheerio');
var request = require('request');
var server = require('./lib/crul')
var i = 0;
var url = "http://www.zcool.com.cn"
    // var nextUrl = "http://list.jd.com"

var filePath = "./data/"

function getContent(x) {
    getPage(x)
}

function getPage(urls) {
    server.download(urls, function(data) {
        if (data) {
            let $ = cheerio.load(data);
            getPageList($);
            var nextLink = $('a.laypage_next').attr('href');
            // console.log(nextLink)
            if (i <= 0) {
                i++
                getContent(nextLink);
            }
        }
    })
}

function getPageList($) {
    $('a.card-img-hover').each(function(index, item) {
        let workLink = $(this).attr('href')
            // console.log($(this).attr('title').trim())

        let fileNameIng = $(this).attr('title').trim();
        let fileName = fileNameIng.replace(/[\ |\~|\`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\-|\_|\+|\=|\||\\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?]/g, "")
        let title = fileName;
        // console.log(fileName)
        fs.mkdir(filePath + fileName, function(err) {
            fs.appendFile(filePath + fileName + '/' + title + '.txt', fileName, 'utf-8', function(err) {
                if (err) {
                    console.log(err)
                }
            })
            server.download(workLink, function(data) {
                if (data) {
                    let $ = cheerio.load(data);
                    fs.mkdir(filePath + fileName + '/img/', function(err) {
                            if (!err) {
                                // let imgs =  $('div.work-show-box').find('img')
                                $('div.work-show-box').find('img').each(function(index, item) {
                                    let imageName = fileName + '-' + index + '.jpg'
                                    let imageSrc = $(this).attr('src')
                                    request.head(imageSrc, function(err, res, body) {
                                        if (err) {
                                            console.log(err);
                                        }
                                        // res.on('end',function(){
                                          
                                        // })
                                        res.on('err', function(err) {
                                            console.log(err)
                                        })
                                        request(imageSrc).pipe(fs.createWriteStream(filePath + fileName + '/img/' + imageName));
                                    });

                                })
                            }
                        })
                        // console.log(title,index)
                }
            })
        })
    })
}

getContent(url)